<?php
// +-------------------------------------------------------------------
// | 
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Admin\Controller;

use Kcdns\Admin\Controller\CommonController;

class ActivityController extends CommonController
{

    public function ranking(){

        $today = date("Y-m-d");

        $today_yugu = M('zs_order_pay')->where(['create_time'=>['egt',$today],'is_pay'=>1])->sum('pay_service_money');

        $data = [
            'today_yugu'=>$today_yugu,
        ];

        $where2['create_time'] = ['create_time'=>['egt',$today],'is_pay'=>1];

        $memberlist = M('member')->select();
        $paihangbang = [];
        if($memberlist){
            foreach ($memberlist as $k =>$v){

                $where2 = array_merge($where2,['mid'=>$v['uid']]);

                $yugu = M('zs_order_pay')->where($where2)->sum('pay_service_money');
                $paihangbang[$k]['username']= $v['nickname'];
                $paihangbang[$k]['money']= $yugu?:0;
            }
        }
        $paihangbang = $this->arraySort($paihangbang,'money',$sort='desc');

        $this->assign('alldata',$data);
        $this->assign('paihangbang',$paihangbang);
        $this->display();
    }


    public function addAct(){

        if (IS_POST) {
            $user = session('user_auth');
            $url = JAVA_API."jDController/getProductInfo";
            $goods_url =  I('post.goods_url');
            $taobao_act_id =  I('post.taobao_act_id');
            $tuanzhang_radio =  I('post.tuanzhang_radio');
            $jieshuquan_img =  I('post.jieshuquan_img');
            $coupon_money =  I('post.coupon_money');
            $coupon_price =  I('post.coupon_price');
            $filename1 =  I('post.filename1');
            $endDate =  I('post.endDate');
            $result = $this->post($url, ['url' => $goods_url]);
            $result = json_decode($result, true);
//            $this->ajaxReturn(['code' => -1, 'msg' => $result['code']], 'json');
            if($result['code'] != 0){
                $this->ajaxReturn(['code' => -1, 'msg' => '未找到商品'], 'json');
            }
            $activity = M('zs_activity')->where(['taobao_act_id' => $taobao_act_id])->find();
            $goods = M('zs_goods')->where([
                'skuId' => $result['data']['skuId'],
                'activity_id' => $activity['id'],
            ])->find();
            if(isset($goods['id']))   $this->ajaxReturn(['code' => -1, 'msg' => '该商品已添加'], 'json');
            $insert = [
                'goods_url' => $result['data']['materialUrl'],
                'logo_img' => $result['data']['imgUrl'],
                'title' => $result['data']['goodsName'],
                'price' => $result['data']['unitPrice'],
                'favourable_price' => $result['data']['unitPrice'],
                'sale' => 0,
                'activity_id' => $activity['id'],
                'commission_fee_old' => $result['data']['commisionRatioWl'],
                'commission_fee' => $result['data']['commisionRatioWl'],
                'service_fee' => 0,
                'remark' => '',
                'mid' => $user['uid'],
                'seller_id' => $result['data']['shopId'],
                'taobao_sku_id' => $result['data']['skuId'],
                'skuId' => $result['data']['skuId'],
                'unitPrice' => $result['data']['unitPrice'],
                'materialUrl' => $result['data']['materialUrl'],
                'endDate' => $endDate,
                'isFreeFreightRisk' => $result['data']['isFreeFreightRisk'],
                'isFreeShipping' => $result['data']['isFreeShipping'],
                'commisionRatioWl' => $result['data']['commisionRatioWl'],
                'commisionRatioPc' => $result['data']['commisionRatioPc'],
                'imgUrl' => $result['data']['imgUrl'],
                'vid' => $result['data']['vid'],
                'cidName' => $result['data']['cidName'],
                'cid' => $result['data']['cid'],
                'cid2' => $result['data']['cid2'],
                'cid2Name' => $result['data']['cid2Name'],
                'cid3' => $result['data']['cid3'],
                'cid3Name' => $result['data']['cid3Name'],
                'wlUnitPrice' => $result['data']['wlUnitPrice'],
                'isSeckill' => $result['data']['isSeckill'],
                'inOrderCount' => $result['data']['inOrderCount'],
                'shopId' => $result['data']['shopId'],
                'isJdSale' => $result['data']['isJdSale'],
                'goodsName' => $result['data']['goodsName'],
                'startDate' => $result['data']['startDate'],
                'tuanzhang_radio' => $tuanzhang_radio,
                'activity_img' => $filename1,
                'jieshuquan_img' => $jieshuquan_img,
                'coupon_money' => $coupon_money,
                'coupon_price' => $coupon_price,
                'taobao_act_id' => $taobao_act_id
            ];
            $insert_id = M('zs_goods')->add($insert);
            if($insert_id){
                $order = M('zs_order_pay')->where(['taobao_act_id' => $taobao_act_id, 'taobao_sku_id' =>$result['data']['skuId']])
                    ->select();
                foreach($order as $k => $v){
                    $save = [
                        'yugu_money' => $v['price']*$tuanzhang_radio/100
                    ];
                    if($v['mid'] == 0) $save['mid'] = $user['uid'];
                    M('zs_order_pay')->where(['id' => $v['id'],
                    ])
                        ->save($save);

                }
                $this->ajaxReturn(['code' => 200, 'msg' => '添加成功'], 'json');
            }else{
                $this->ajaxReturn(['code' => -1, 'msg' => '添加产品失败'], 'json');
            }
        } else {
            $activity = M('zs_activity')->where(['id' => I('get.zs_activity.id')])->find();
            $this->assign('activity', $activity);
            $this->display();
        }
    }

    public function addSeller(){

        if (IS_POST) {
            $taobao_act_id =  I('post.taobao_act_id');
            $user = session('user_auth');
            $activity = M('zs_activity')->where(['taobao_act_id' => $taobao_act_id])->find();
            if(!isset($activity['id']))$this->ajaxReturn(['code' => -1, 'msg' => '活动不存在'], 'json');
            $insert = [
                'taobao_act_id' => $activity['taobao_act_id'],
                'shopId' => I('post.shopId'),
                'seller_nickname' => I('post.seller_nickname'),
                'seller_title' => I('post.seller_title'),
                'type' => I('post.type'),
                'url' => I('post.url'),
                'qq' => I('post.qq'),
                'weixin_name' => I('post.weixin_name'),
                'operation' => I('post.operation'),
                'mobile' => I('post.mobile'),
                'remark' => I('post.remark'),
                'is_hezuo' => I('post.is_hezuo'),
                'create_time' => date('Y-m-d H:i:s'),
                'mid' => $user['uid'],

            ];
            $insert_id = M('zs_seller')->add($insert);
            if($insert_id){
                $this->ajaxReturn(['code' => 200, 'msg' => '添加店铺成功'], 'json');
            }else{
                $this->ajaxReturn(['code' => -1, 'msg' => '添加店铺失败'], 'json');
            }
        } else {
            $activity = M('zs_activity')->where(['id' => I('get.zs_activity.id')])->find();
            $this->assign('activity', $activity);
            $this->display();
        }
    }

    public function upload(){
        if (IS_POST) {
            $ss =   $_FILES;

        }
    }

    public function addActDeal(){

    }

    public function getGoodsDetail(){

        $url = JAVA_API."jDController/getProductInfo";
        $goods_url =  I('post.goods_url');
        $result = $this->post($url, ['url' => $goods_url]);
        $result = json_decode($result, true);
        if($result['code'] == 0){
            $result['code'] = 200;
        }

        $this->ajaxReturn($result, 'json');
    }

    public function post($url, $data) {

        //初使化init方法
        $ch = curl_init();
        //指定URL
        curl_setopt($ch, CURLOPT_URL, $url);
        //设定请求后返回结果
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //声明使用POST方式来进行发送
        curl_setopt($ch, CURLOPT_POST, 1);
        //发送什么数据呢
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        //忽略证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //忽略header头信息
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //设置超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        //发送请求
        $output = curl_exec($ch);
        //关闭curl
        curl_close($ch);
        //返回数据
        return $output;
    }


    
}